package com.zhongyouyun.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.admin.model.DictModel;
import com.zhongyouyun.admin.model.DictionaryDetailedModel;
import com.zhongyouyun.admin.model.DictionaryModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.pojo.LogPojo;
import com.zhongyouyun.other.annotation.SysLogAnnotation;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhongyouyun.admin.service.DictionaryService;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: shenyi
 * @Date: 2023-02-01
 * @Description:
 */
@RestController
@RequestMapping("/admin/z_dictionary")
@Api(value = "基础字典管理", tags = {"基础字典管理接口"})
public class DictionaryController {
    @Autowired
    private DictionaryService service;

    @GetMapping("/findList")
    @ResponseBody
    @ApiOperation(value = "查询字典列表", notes = "通过条件查询字典列表")
    @SysLogAnnotation(operModul = "系统管理>>字典管理", operType = "字典列表", operDesc = "日志列表")
    public List<DictionaryModel> findPage(@ApiParam(name = "字段名", value = "name", required = false) @RequestParam(value = "name") String name) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StrUtil.isNotEmpty(name)) {
            queryWrapper.like("field_name", name);
        }
        return service.selectList(queryWrapper);
    }

    @PostMapping("/saveOrUpate")
    @ResponseBody
    @ApiOperation(value = "修改或者新增", notes = "通过ID判断修改或者新增")
    public Result saveOrUpate(@RequestBody DictionaryModel dictionaryModel) {
        UserModel userModel = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        if (ObjectUtil.isNotEmpty(dictionaryModel.getId()) && ObjectUtil.isNotEmpty(dictionaryModel.getId())) {
            userModel.setUpdateBy(userModel.getId().toString());
        } else {
            ModelUtils modelutils = new ModelUtils();
            modelutils.initModel(dictionaryModel, userModel.getId().toString());
        }
        Boolean flag = service.saveOrUpdate(dictionaryModel);
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("保存失败").success(false).status(1).build();
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除", notes = "通过ID删除")
    public Result delete(@ApiParam(name = "字典ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        Boolean flag = false;
        flag = service.removeById(Long.valueOf(id));
        if (flag) {
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }

    @PostMapping("/batchDelete")
    @ResponseBody
    @ApiOperation(value = "批量删除", notes = "通过ID删除")
    public Result batchDelete(@RequestBody List<String> ids) {
        List<Long> list = new ArrayList<Long>();
        ids.forEach(id -> {
            list.add(Long.valueOf(id));
        });
        Boolean flag = service.removeByIds(list);
        if (flag) {
            //删除子表
            return Result.builder().message("成功").status(0).build();
        } else {
            return Result.builder().message("删除失败").success(false).status(1).build();
        }
    }


}
